package com.cspg.ssss.service;

import com.cspg.ssss.domain.AppSecrtEntity;
import com.cspg.ssss.repository.AppSecrtRepository;
import com.cspg.ssss.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class AppSecrtService {

    @Autowired
    AppSecrtRepository repository;

    public String generateAppSecrt(String app_name){
        String app_secrt = generateSecrt(app_name);
        AppSecrtEntity en = new AppSecrtEntity();
        en.setApp_name(app_name);
        en.setApp_secrt(app_secrt);
        repository.insertAppSecrt(en);
        return en.getApp_secrt();

    }

    private String generateSecrt(String keyname){
        long totalMilliSeconds = System.currentTimeMillis();
        String mw = "key:" + keyname + ":"+String.valueOf(totalMilliSeconds);
        String app_secrt = MD5Util.getMd5(mw);
        return app_secrt;
    }

    /**
     *
     *
     */
    public String updateAppSecrt(String app_name){
        HashMap<String,Object> updateFiled = new HashMap<String, Object>();
        updateFiled.put("app_name",app_name);
        String app_secrt = generateSecrt(app_name);
        updateFiled.put("app_secrt",app_secrt);
        repository.updateAppSecrt(updateFiled);
        return app_secrt;
    }

    public AppSecrtEntity getAppSecrtByAppname(String app_name){
        return repository.findAppSecrtByAppname(app_name);
    }
}
